package main.leetcode.offer.secondround.from03to50;

import java.util.Deque;
import java.util.LinkedList;

/** 09.用两个栈实现队列 */
public class ex09 {
    /** stack1 for push */
    private Deque<Integer> stack1;
    /** stack2 for pop */
    private Deque<Integer> stack2;

    public ex09() {
        stack1 = new LinkedList<>();
        stack2 = new LinkedList<>();
    }

    public static void main(String[] args) {
        ex09 queue = new ex09();
        System.out.println(queue.deleteHead());
        queue.appendTail(5);
        queue.appendTail(2);
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
    }

    public void appendTail(int value) {
        stack1.addLast(value);
    }

    public int deleteHead() {
        if (stack1.isEmpty() && stack2.isEmpty()) {
            return -1;
        }
        // stack2 为空，将stack1中的数据移到 stack2
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.addLast(stack1.pollLast());
            }
        }
        return stack2.pollLast();
    }
}
